var qygl_app = function() {//企业管理
	var path =Constants.amUrlPath;
	var ractive;
	var qyglApp ={
		URL:{
			list: function(){//分页查询路径post
				return path + "qygl/getQyxxPagination";
			},
			toEdit: function(id){//新增和修改html页面
				return "qygl-xz.html?qyid=" + id;
			},
			disable: function(id,zzjgdm){//禁用get
				return path + "qygl/disableQyxx?qyid="+id +"&zzjgdm=" + zzjgdm;
			},
			recover: function(id,zzjgdm){//启用get
				return path + "qygl/recoverQyxx?qyid="+id +"&zzjgdm=" + zzjgdm;
			},
			reset: function(glyzh,newPwd){//重置密码get
				return path + "qygl/resetPassword?glyzh="+glyzh +"&newPwd=" + newPwd;
			},
			selectbox: function(codeClass){
				return path + "common/getCodeListByCodeClass?codeClass=" + codeClass;
			}
		},
		getSelectedZtVal: function(codeClass,eleId){
	        $.ajax({
				type: "get",
				url: qyglApp.URL.selectbox(codeClass),				
				async: false,
				success: function(data) {//数据绑定
					data.unshift({"code": -1, "codedesc": "全部"});
					store.set(eleId, JSON.stringify(data));
					selecttextInfo();
					setWDCode("code_"+eleId, "-1");
				}
    	    })
    	    return $('#'+eleId).val(); 
		},
		validateStr: function(str,obj,id,errText){//非空验证
	    	 if ($.trim(str) == ''){
	    		 obj.document.getElementById(id).innerHTML = errText;
	    		 obj.document.getElementById(id).style.display = "inline-block";
	             return false;
	         } else {
	        	 obj.document.getElementById(id).innerHTML = '';
	        	 obj.document.getElementById(id).style.display = "none";
	             return true;
	         }
	    },
		showResetpwd: function(glyzh,newPwd){//显示重置密码弹出层
			manager.isLogin();
	    	var dialogObjs = {//弹出框参数信息
				title: "重置密码",
				width: 680,
				height: 240,
				innerhtml: null,
				headbuttonareaflg: true,
				buttonareaflg: true,
				url: "qygl-reset.html",
				successCallBackFun: showDialogSuccess,
				cancelCallBackFun: showDialogCancel,
				glyzh: glyzh
			}
	    	var dis = dialog(dialogObjs);//弹出层对象
	    	store.set('qygl_glyzh',glyzh);
	    	function showDialogSuccess(){//成功回调函数
	    		//console.log(dis.Win.document);
	    		var pwd = dis.Win.document.getElementById('pwd').value;
	    		if(!qyglApp.validateStr(pwd,dis.Win,'pwdcerr','密码不能为空！')){return};
	    		if(!(pwd.length >= 6)){//
	    			qyglApp.validateStr('',dis.Win,'pwdcerr','密码为6-20个字符！');
					return;
				}
	    		qyglApp.resetPwd(dialogObjs.glyzh,pwd);
	    		//关闭弹出层
				wdDialogClosed("dialog");			
			}
	    	//取消回调函数
			function showDialogCancel(){
				wdDialogClosed("dialog");//关闭弹出层
			}
		},
		resetPwd: function(glyzh,newPwd){//重置密码
			$.get(qyglApp.URL.reset(glyzh,newPwd),function(result){
				if(!result.result){//失败提示
					alert({txt: "重置密码失败！",callBackFun: callBackSuccessFun});					
					function callBackSuccessFun(){}
				}else{//成功提示
					var objs = {txt:"重置密码成功！",callBackFun:fn}
					success(objs);function fn() {}
				}	
			})
		}
	}
	
	//页面渲染
	var init_param = {"currPage" : 1,"pageSize" : 20};
	function renderView() {//渲染页面
		ractive = new Ractive({
			el : ".hp-container",
			template : "#qygl-template",
			oncomplete : function() {
				init_param.zt = qyglApp.getSelectedZtVal('QYZT','QYZT'); //企业状态下拉列表  
				initTable(init_param);//初始化表格
			}
		});
		ractive.on({
			search: function() {//查询
				init_param.zt = $("#QYZT").val();
				var searchParam = {qymc: ractive.get("qymc") ? $.trim(ractive.get("qymc")) : ""};
				ractive.set("qymc",(ractive.get("qymc") ? $.trim(ractive.get("qymc")) : ""));
				var objs = $.extend(init_param, searchParam);//合并参数
				initTable(objs);//查询后加载表格数据
			},
			xzqy: function(){//新增
				manager.isLogin();
				location.href = qyglApp.URL.toEdit('');
			}
		});
	}
	//加载表格数据
	function initTable(param) {
		var _table_title =  ['企业名称', '企业代码', '企业域名', '管理员账号', '状态', '创建时间', '操作'];
		var _col_model = [
	        {name: 'qymc',width: 100,float:"left"},
			{name: 'zzjgdm',width: 140},
			{name: 'qyym',width: 180,float:"left",
				innerhtml: function(opts){
					return  "<div style='width:180px;height: 26px; line-height: 26px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;' title='http://"+opts.qyym+".wdcloud.cc'>http://" + opts.qyym + ".wdcloud.cc</div>";
				}
			},
			{name: 'glyzh',width: 80},
			{name: 'ztDesc',width: 60},
			{name: 'cjsj',width: 160,
				innerhtml: function(opts){
					return   moment(opts.cjsj).format("YYYY-MM-DD HH:mm:ss")
				}
			},
			{name: 'cz',width: 280,
				innerhtml: function(opts) {				
					var str = '';
					
					if(opts.zt == Constants.code.QYGL.QYZT_ZC){
						str += '<span class="ad-gridlist-btn btn2" onclick="qygl_app.disable(\'' + opts.id +'\',\''+ opts.zzjgdm + '\')"  />禁用</span>';	
					}
					if(opts.zt == Constants.code.QYGL.QYZT_TY){
						str += '<span class="ad-gridlist-btn" onclick="qygl_app.recover(\'' + opts.id  +'\',\''+ opts.zzjgdm + '\')" />启用</span>';	
					}
					str += '<span class="ad-gridlist-btn" onclick="qygl_app.reset(\'' + opts.glyzh  +'\')" />重置管理员密码</span>';
					str += '<span class="ad-gridlist-btn"  onclick="qygl_app.update(\'' + opts.id + '\')"  />修改</span>';
						
					return str;
				}
			}
		];
		jQuery("#wd-table").wdGrid({
			url: qyglApp.URL.list(),
			colNames: _table_title,
			colModel: _col_model,
			dataType: "GET",
			submitType: "noForm",
			postData: param,
			pager: "#wd-pager",
			rowList: Constants.page.rowList,
			pagesize: Constants.page.pagesize
		});
	}
	
	function update(id) {//修改操作
		location.href =  qyglApp.URL.toEdit(id);
	}
	
	function disable(id,zzjgdm) {//禁用操作
		confirm({txt: "确认禁用吗？",successCallBackFun: successCallBackFun,cancelCallBackFun: cancelCallBackFun});	
		function successCallBackFun(){//确认
			$.get(qyglApp.URL.disable(id,zzjgdm),function(data){
				if(data.result){
					var objs = {txt:"禁用成功！",callBackFun:function(){window.location.reload()}}
					success(objs);
				}else{
					manager.errorMsg(data, null);
				}
			})
		}
		function cancelCallBackFun(){}//取消
	}
	
	function recover(id,zzjgdm) {//启用操作
		confirm({txt: "确认启用吗？",successCallBackFun: successCallBackFun,cancelCallBackFun: cancelCallBackFun});	
		function successCallBackFun(){//确认
			$.get(qyglApp.URL.recover(id,zzjgdm),function(data){
				if(data.result){
					var objs = {txt:"启用成功！",callBackFun:function(){window.location.reload()}}
					success(objs);
				}else{
					manager.errorMsg(data, null);
				}
				
				
			})
		}
		function cancelCallBackFun(){}//取消
	}
	
	function reset(glyzh) {//重置密码操作
		qyglApp.showResetpwd(glyzh);//显示弹出层
		//qyglApp.resetPwd(glyzh);//直接提交
	}
	
	return {
		init : function() {
			renderView();
		},
		update : function(id) {
			update(id);
		},
		disable: function(id,zzjgdm){
			disable(id,zzjgdm);
		},
		recover: function(id,zzjgdm){
			recover(id,zzjgdm);
		},
		reset: function(glyzh){
			reset(glyzh);
		}
	};
}();
$(document).ready(qygl_app.init);
